package com.imooc.article;

import com.imooc.api.config.RabbitMQConfig;
import com.imooc.api.config.RabbitMQDelayConfig;
import com.imooc.article.service.ArticleService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Project_name: imooc-news-dev
 * @Description:
 * @Author: Levi
 * @Date: 2021/3/29 19:44
 */
@Component
public class RabbitMQDelayConsumer {

    @Autowired
    private ArticleService articleService;


    @RabbitListener(queues = RabbitMQDelayConfig.QUEUE_DELAY)
    public void watchQueue(String payload, Message message) {

//        System.out.println(payload);
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();

        System.out.println("消费者接收消息时间: "+new Date());
        if (routingKey.equalsIgnoreCase("delay.publisharticle")) {
            String articleId = payload;
            articleService.updateArticleToPublish(articleId);
        }
    }
}
